"""
@author: huangll
@email: ll.huang@hzgosun.com
@time: 2019/7/23 15:56

单兵管理的增改删查自动化测试用例编写
"""

import os
import unittest
from time import sleep
from selenium.webdriver.common.keys import Keys
from common.functions import save_screenshot
from common.decorators import mkdir_classname, log4gs
from common.settings import TAG, DATA_DIR
from common.vue.select import DropDownSelect, OperateSelect
from pages.individualPage import IndividualPage


@log4gs
@mkdir_classname(TAG)
class IndividualTest(unittest.TestCase):
    """单兵管理功能测试"""
    @classmethod
    def setUpClass(cls):
        cls.log.info('=' * 30 + "开始单兵管理功能测试({})".format(__class__.__name__) + '=' * 30)
        cls.page = IndividualPage('/DevManage/Individual')

    @classmethod
    def tearDownClass(cls):
        cls.log.info('=' * 30 + "结束单兵管理功能测试({})".format(__class__.__name__) + '=' * 30)
        cls.page.driver.quit()

    def setUp(self):
        self.log.info('>>>>>开始执行用例：{} {}'.format(self.id(), self.shortDescription()))
        sleep(1)

    def tearDown(self):
        self.log.info('<<<<<结束执行用例：{} {}'.format(self.id(), self.shortDescription()))

    @unittest.skip
    def test_1_page(self):
        """翻页测试"""
        sleep(1)
        self.log.debug("首页共有：{}页数据".format(self.page.turnPage.total_page))
        self.log.debug("当前处于第：{}页数据".format(self.page.turnPage.current_page))
        self.log.debug("共有：{}条数据".format(self.page.turnPage.total_num))
        sleep(2)
        self.assertNotEqual(self.page.turnPage.total_page, 1, msg="页数过少，无法进行翻页测试")
        try:
            self.page.turnPage.next_page.click()
            self.assertEqual(self.page.turnPage.current_page, '2')
            sleep(2)
            try:
                self.page.turnPage.next_page.click()
                self.assertEqual(self.page.turnPage.current_page, '2')
                sleep(2)
                self.page.turnPage.prev_page.click()
                self.assertEqual(self.page.turnPage.current_page, '1')
                sleep(2)
                self.page.turnPage.input.clear()
                self.page.turnPage.input.send_keys(2)
                self.page.turnPage.input.send_keys(Keys.ENTER)
                self.assertEqual(self.page.turnPage.current_page, '2')
            except Exception as e:
                print("无法执行翻页测试" % e)
        except Exception as e:
            print("翻页测试用例执行失败:%r" % e)
            save_screenshot(self, "翻页功能测试失败")

    @unittest.skip
    def test_2_add_individual(self):
        """添加单兵"""
        try:
            self.page.table.add_btn.click()
            sleep(1)
            self.page.addindividual.access_code.send_keys("测试自动化1")
            self.page.addindividual.access_id.send_keys("33333")
            # 所属人和所属岗亭只能填写一个,且其中一个必填
            DropDownSelect(self.page.addindividual.individual_person, "都市花园班长")  # 所属人编辑
            # DropDownSelect(self.page.addindividual.sentry_box, "中央公园小区")  # 所属岗亭编辑
            sleep(1)
            self.page.addindividual.confirm.click()
            # 检查是否添加成功
            sleep(1)
            self.page.searchTools.search_access_code.send_keys("测试自动化1")  # 选择单兵查询码查询
            self.page.searchTools.search_btn.click()  # 点击检索按钮
            sleep(1)
            result = self.page.table.get_row("测试自动化1")
            self.assertTrue(result, "通过单兵查询码:测试自动化1查询到了测洁自动化1")
            sleep(1)
        except Exception as e:
            print("添加单兵用例执行失败:%r" % e)
            save_screenshot(self, "添加单兵失败")

    @unittest.skip
    def test_3_alter_individual(self):
        """修改单兵"""
        try:
            sleep(1)
            self.page.searchTools.search_access_code.clear()
            self.page.searchTools.search_access_code.send_keys("测试自动化1")  # 选择单兵查询码查询
            self.page.searchTools.search_btn.click()  # 点击检索按钮
            sleep(1)
            result = self.page.table.get_row("测试自动化1")
            OperateSelect(result,"编辑")
            sleep(1)
            self.page.alterindividual.access_code.clear()
            self.page.alterindividual.access_code.send_keys("百花缭乱的单兵")  # 修改单兵查询码
            self.page.alterindividual.access_id.clear()
            self.page.alterindividual.access_id.send_keys("33335")  # 修改单兵账号
            # self.page.alterindividual.individual_person.clear()  # 清除单兵所属人
            # sleep(1)
            # self.page.alterindividual.sentry_box.send_keys("中央公园小区")  # 绑定岗亭
            sleep(1)
            self.page.alterindividual.confirm.click()
            sleep(2)
            self.page.searchTools.search_access_code.clear()
            self.page.searchTools.search_access_code.send_keys("百花缭乱的单兵")
            self.page.searchTools.search_btn.click()
            sleep(1)
            result = self.page.table.get_row("百花缭乱的单兵")
            self.assertTrue(result, "通过单兵查询码:百花缭乱的单兵查询到了百花缭乱的单兵")
        except Exception as e:
            print("修改单兵用例执行失败：%r" % e)
            save_screenshot(self,"修改单兵失败")

    @unittest.skip
    def test_4_delete_individual_code(self):
        """删除单兵"""
        try:
            sleep(1)
            self.page.searchTools.search_access_code.clear()
            self.page.searchTools.search_access_code.send_keys("百花缭乱的单兵")  # 选择单兵查询码查询
            self.page.searchTools.search_btn.click()  # 点击检索按钮
            sleep(1)
            self.page.searchTools.search_access_code.clear()
            result = self.page.table.get_row("百花缭乱的单兵")
            OperateSelect(result, "删除")
            sleep(1)
            self.page.deleteindividual.confirm.click()
        except Exception as e:
            print("删除单兵用例执行失败：%r" % e)
            save_screenshot(self, "删除单兵失败")

    def test_5_search_individual(self):
        """单兵查询码检索"""
        try:
            sleep(1)
            self.page.searchTools.search_access_code.send_keys("新兵")
            self.page.searchTools.search_btn.click()  # 点击检索按钮
            sleep(1)

            # self.page.searchTools.search_access_code.clear()  # 清除单兵查询码输入框数据，用下面两行代替清除数据
            self.page.searchTools.search_access_code.send_keys(Keys.CONTROL, 'a')
            self.page.searchTools.search_access_code.send_keys(Keys.DELETE)

            result = self.page.table.get_row("新兵")
            self.assertTrue(result, "通过单兵查询码:新兵查询到了新兵")
            sleep(1)
        except Exception as e:
            print("单兵查询码检索用例执行失败：%r" % e)
            save_screenshot(self, "单兵查询码检索失败")

    @unittest.skip
    def test_6_search_individual_id(self):
        """单兵账号"""
        try:
            sleep(1)
            self.page.searchTools.search_access_id.send_keys("新兵2号")
            self.page.searchTools.search_btn.click()  # 点击检索按钮
            sleep(1)
            self.page.searchTools.search_access_id.clear()
            sleep(1)
            result = self.page.table.get_row("新兵2号")
            self.assertTrue(result, "通过单兵账号:新兵2号查询到了新兵2号")
            sleep(2)
            self.page.searchTools.search_btn.click()  # 点击检索按钮  i
        except Exception as e:
            print("单兵账号检索用例执行失败：%r" % e)
            save_screenshot(self,"单兵账号检索失败")

    @unittest.skip
    def test_7_search_individual_person(self):
        """所属人检索"""
        try:
            sleep(1)
            self.page.searchTools.search_individual_person.send_keys("蓝色海云居保安3号")
            self.page.searchTools.search_btn.click()  # 点击检索按钮
            sleep(1)
            self.page.searchTools.search_individual_person.clear()
            sleep(1)
            result = self.page.table.get_row("蓝色海云居保安3号")
            self.assertTrue(result, "通过所属人:蓝色海云居保安3号查询到了蓝色海云居保安3号")
            sleep(2)
        except Exception as e:
            print("所属人检索用例执行失败：%r" % e)
            save_screenshot(self,"所属人检索失败")

    @unittest.skip
    def test_8_search_individual_sentry_box(self):
        """岗亭检索"""
        try:
            sleep(1)
            self.page.searchTools.search_sentry_box.send_keys("都市花园小区岗亭")
            self.page.searchTools.search_btn.click()  # 点击检索按钮
            sleep(1)
            self.page.searchTools.search_sentry_box.clear()
            sleep(1)
            result = self.page.table.get_row("都市花园小区岗亭")
            self.assertTrue(result, "通过岗亭:都市花园小区岗亭查询到了都市花园小区岗亭")
            sleep(1)
        except Exception as e:
            print("岗亭检索用例执行失败：%r" % e)
            save_screenshot(self,"岗亭检索失败")


if __name__ == '__main__':
    unittest.main()
    # suit = unittest.TestSuite()
    # suit.addTest(IndividualTest.test_5_search_individual)
    # suit.addTest(IndividualTest.test_6_search_individual_id)
    # runner = unittest.TextTestRunner()
    # runner.run(suit)